<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理 -->
      <el-row class="btn_container">
        <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>
      </el-row>
      <!-- 表格组件 -->
      <el-table v-loading="loading" :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色" />
        <el-table-column prop="state" align="center" width="200" label="启用">
          <!-- 自定义列解构 -->
          <template v-slot="{row}">
            <span>{{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <!-- 放置操作按钮 -->
          <template>
            <el-button size="mini" type="text">分配权限</el-button>
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row style="height: 80px;" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager, next"
          :total="50"
        />
        <el-dialog title="新增角色" :visible.sync="showDialog" @click="btnCancel">
          <el-form ref="addRole" label-width="80px" :model="addForm" placeholder="请输入用户名" :rules="addRules">
            <el-form-item prop="name" label="角色名称">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item prop="state">
              <el-switch v-model="addForm.state" />
            </el-form-item>
            <el-form-item prop="description" label="角色描述">
              <el-input v-model="addForm.description" placeholder="请输入角色描述" />
            </el-form-item>
            <el-form-item>
              <el-row type="flex" justify="center">
                <el-button size="mini" @click="btnCancel">取消</el-button>
                <el-button size="mini" @click="btnOK">确定</el-button>
              </el-row>
            </el-form-item>
          </el-form>
        </el-dialog>
      </el-row>

    </div>
  </div>
</template>
<script>
import { getRoleListAPI, addRoleAPI } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      loading: false,
      list: [],
      pageParams: {
        total: 0,
        page: 1, // 默认请求第一页
        pagesize: 5 // 每页5条
      },
      showDialog: false,
      addRules: {
        name: [{ required: true, message: '角色名称不能为空' }],
        description: [{ required: true, message: '角色描述不能为空' }]
      },
      addForm: {
        name: '',
        description: '',
        state: 0
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async btnOK() {
      await this.$refs.addRole.validate()
      await addRoleAPI(this.addForm)
      this.$message.success('新增角色成功')
      this.btnCancel()
      this.getRoleList() // 重新获取数据
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.addRole.reseFields()
    },
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleListAPI(this.pageParams)
      this.list = rows
      this.pageParams.total = total
      this.loading = false
    }
  }
}
</script>
<style>
.btn_container{
  padding: 10px 20px;
}
</style>
